Imports cmplib01

Public Class frmAssignToCombats
    Inherits System.Windows.Forms.Form

    Private c_Module As String = "frmAssignToCombats"

    Private ptrGameData As CampaignerGameData
    Private ptrSelectedBattle As Battle
    Private ptrSelectedPlayer As Player

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents pnlTopBorder As System.Windows.Forms.Panel
    Friend WithEvents picCampaignerIcon As System.Windows.Forms.PictureBox
    Friend WithEvents txtTitleBox As System.Windows.Forms.TextBox
    Friend WithEvents lstExistingBattles As System.Windows.Forms.ListBox
    Friend WithEvents lblSelectBattle As System.Windows.Forms.Label
    Friend WithEvents lstPlayer As System.Windows.Forms.ListBox
    Friend WithEvents lblSelectAPlayer As System.Windows.Forms.Label
    Friend WithEvents tvwInvolvedChits As System.Windows.Forms.TreeView
    Friend WithEvents lblReinforcingUnits As System.Windows.Forms.Label
    Friend WithEvents lblSelectedRules As System.Windows.Forms.Label
    Friend WithEvents lstAssignedRules As System.Windows.Forms.ListBox
    Friend WithEvents tvwAvailableRules As System.Windows.Forms.TreeView
    Friend WithEvents tvwReinforcements As System.Windows.Forms.TreeView
    Friend WithEvents pnlMain As System.Windows.Forms.Panel
    Friend WithEvents lblAvailableRules As System.Windows.Forms.Label
    Friend WithEvents lblInvolvedUnits As System.Windows.Forms.Label
    Friend WithEvents lblObjective As System.Windows.Forms.Label
    Friend WithEvents tvwObjective As System.Windows.Forms.TreeView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmAssignToCombats))
        Me.pnlMain = New System.Windows.Forms.Panel()
        Me.tvwObjective = New System.Windows.Forms.TreeView()
        Me.lblObjective = New System.Windows.Forms.Label()
        Me.lblInvolvedUnits = New System.Windows.Forms.Label()
        Me.tvwReinforcements = New System.Windows.Forms.TreeView()
        Me.tvwAvailableRules = New System.Windows.Forms.TreeView()
        Me.lstAssignedRules = New System.Windows.Forms.ListBox()
        Me.lblAvailableRules = New System.Windows.Forms.Label()
        Me.lblReinforcingUnits = New System.Windows.Forms.Label()
        Me.lblSelectedRules = New System.Windows.Forms.Label()
        Me.tvwInvolvedChits = New System.Windows.Forms.TreeView()
        Me.lblSelectAPlayer = New System.Windows.Forms.Label()
        Me.lstPlayer = New System.Windows.Forms.ListBox()
        Me.lblSelectBattle = New System.Windows.Forms.Label()
        Me.lstExistingBattles = New System.Windows.Forms.ListBox()
        Me.btnOK = New System.Windows.Forms.Button()
        Me.pnlTopBorder = New System.Windows.Forms.Panel()
        Me.picCampaignerIcon = New System.Windows.Forms.PictureBox()
        Me.txtTitleBox = New System.Windows.Forms.TextBox()
        Me.pnlMain.SuspendLayout()
        Me.pnlTopBorder.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlMain
        '
        Me.pnlMain.BackColor = System.Drawing.Color.LightSkyBlue
        Me.pnlMain.Controls.AddRange(New System.Windows.Forms.Control() {Me.tvwObjective, Me.lblObjective, Me.lblInvolvedUnits, Me.tvwReinforcements, Me.tvwAvailableRules, Me.lstAssignedRules, Me.lblAvailableRules, Me.lblReinforcingUnits, Me.lblSelectedRules, Me.tvwInvolvedChits, Me.lblSelectAPlayer, Me.lstPlayer, Me.lblSelectBattle, Me.lstExistingBattles})
        Me.pnlMain.Location = New System.Drawing.Point(8, 48)
        Me.pnlMain.Name = "pnlMain"
        Me.pnlMain.Size = New System.Drawing.Size(496, 488)
        Me.pnlMain.TabIndex = 20
        '
        'tvwObjective
        '
        Me.tvwObjective.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.tvwObjective.ImageIndex = -1
        Me.tvwObjective.Location = New System.Drawing.Point(248, 160)
        Me.tvwObjective.Name = "tvwObjective"
        Me.tvwObjective.SelectedImageIndex = -1
        Me.tvwObjective.ShowRootLines = False
        Me.tvwObjective.Size = New System.Drawing.Size(240, 96)
        Me.tvwObjective.TabIndex = 24
        '
        'lblObjective
        '
        Me.lblObjective.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblObjective.ForeColor = System.Drawing.Color.Black
        Me.lblObjective.Location = New System.Drawing.Point(256, 136)
        Me.lblObjective.Name = "lblObjective"
        Me.lblObjective.Size = New System.Drawing.Size(168, 24)
        Me.lblObjective.TabIndex = 23
        Me.lblObjective.Text = "Involved Objective"
        '
        'lblInvolvedUnits
        '
        Me.lblInvolvedUnits.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblInvolvedUnits.ForeColor = System.Drawing.Color.Black
        Me.lblInvolvedUnits.Location = New System.Drawing.Point(16, 136)
        Me.lblInvolvedUnits.Name = "lblInvolvedUnits"
        Me.lblInvolvedUnits.Size = New System.Drawing.Size(144, 21)
        Me.lblInvolvedUnits.TabIndex = 22
        Me.lblInvolvedUnits.Text = "Involved Chits"
        '
        'tvwReinforcements
        '
        Me.tvwReinforcements.CheckBoxes = True
        Me.tvwReinforcements.ImageIndex = -1
        Me.tvwReinforcements.Location = New System.Drawing.Point(8, 392)
        Me.tvwReinforcements.Name = "tvwReinforcements"
        Me.tvwReinforcements.SelectedImageIndex = -1
        Me.tvwReinforcements.ShowRootLines = False
        Me.tvwReinforcements.Size = New System.Drawing.Size(208, 88)
        Me.tvwReinforcements.TabIndex = 21
        '
        'tvwAvailableRules
        '
        Me.tvwAvailableRules.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.tvwAvailableRules.ImageIndex = -1
        Me.tvwAvailableRules.Location = New System.Drawing.Point(253, 288)
        Me.tvwAvailableRules.Name = "tvwAvailableRules"
        Me.tvwAvailableRules.SelectedImageIndex = -1
        Me.tvwAvailableRules.ShowRootLines = False
        Me.tvwAvailableRules.Size = New System.Drawing.Size(235, 77)
        Me.tvwAvailableRules.TabIndex = 20
        '
        'lstAssignedRules
        '
        Me.lstAssignedRules.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstAssignedRules.ItemHeight = 15
        Me.lstAssignedRules.Location = New System.Drawing.Point(8, 288)
        Me.lstAssignedRules.Name = "lstAssignedRules"
        Me.lstAssignedRules.Size = New System.Drawing.Size(235, 77)
        Me.lstAssignedRules.TabIndex = 16
        '
        'lblAvailableRules
        '
        Me.lblAvailableRules.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblAvailableRules.ForeColor = System.Drawing.Color.Black
        Me.lblAvailableRules.Location = New System.Drawing.Point(264, 264)
        Me.lblAvailableRules.Name = "lblAvailableRules"
        Me.lblAvailableRules.Size = New System.Drawing.Size(168, 16)
        Me.lblAvailableRules.TabIndex = 14
        Me.lblAvailableRules.Text = "Available Rules"
        '
        'lblReinforcingUnits
        '
        Me.lblReinforcingUnits.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblReinforcingUnits.ForeColor = System.Drawing.Color.Black
        Me.lblReinforcingUnits.Location = New System.Drawing.Point(16, 368)
        Me.lblReinforcingUnits.Name = "lblReinforcingUnits"
        Me.lblReinforcingUnits.Size = New System.Drawing.Size(128, 16)
        Me.lblReinforcingUnits.TabIndex = 11
        Me.lblReinforcingUnits.Text = "Reinforcements"
        '
        'lblSelectedRules
        '
        Me.lblSelectedRules.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblSelectedRules.ForeColor = System.Drawing.Color.Black
        Me.lblSelectedRules.Location = New System.Drawing.Point(16, 264)
        Me.lblSelectedRules.Name = "lblSelectedRules"
        Me.lblSelectedRules.Size = New System.Drawing.Size(136, 21)
        Me.lblSelectedRules.TabIndex = 9
        Me.lblSelectedRules.Text = "Assigned Rules"
        '
        'tvwInvolvedChits
        '
        Me.tvwInvolvedChits.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.tvwInvolvedChits.ImageIndex = -1
        Me.tvwInvolvedChits.Location = New System.Drawing.Point(8, 160)
        Me.tvwInvolvedChits.Name = "tvwInvolvedChits"
        Me.tvwInvolvedChits.SelectedImageIndex = -1
        Me.tvwInvolvedChits.ShowRootLines = False
        Me.tvwInvolvedChits.Size = New System.Drawing.Size(232, 96)
        Me.tvwInvolvedChits.TabIndex = 6
        '
        'lblSelectAPlayer
        '
        Me.lblSelectAPlayer.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblSelectAPlayer.ForeColor = System.Drawing.Color.Black
        Me.lblSelectAPlayer.Location = New System.Drawing.Point(16, 75)
        Me.lblSelectAPlayer.Name = "lblSelectAPlayer"
        Me.lblSelectAPlayer.Size = New System.Drawing.Size(144, 21)
        Me.lblSelectAPlayer.TabIndex = 5
        Me.lblSelectAPlayer.Text = "Select A Player"
        '
        'lstPlayer
        '
        Me.lstPlayer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstPlayer.ItemHeight = 15
        Me.lstPlayer.Location = New System.Drawing.Point(8, 96)
        Me.lstPlayer.Name = "lstPlayer"
        Me.lstPlayer.Size = New System.Drawing.Size(480, 32)
        Me.lstPlayer.TabIndex = 4
        '
        'lblSelectBattle
        '
        Me.lblSelectBattle.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.lblSelectBattle.ForeColor = System.Drawing.Color.Black
        Me.lblSelectBattle.Location = New System.Drawing.Point(16, 3)
        Me.lblSelectBattle.Name = "lblSelectBattle"
        Me.lblSelectBattle.Size = New System.Drawing.Size(136, 16)
        Me.lblSelectBattle.TabIndex = 3
        Me.lblSelectBattle.Text = "Select A Battle"
        '
        'lstExistingBattles
        '
        Me.lstExistingBattles.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lstExistingBattles.ItemHeight = 15
        Me.lstExistingBattles.Location = New System.Drawing.Point(8, 24)
        Me.lstExistingBattles.Name = "lstExistingBattles"
        Me.lstExistingBattles.Size = New System.Drawing.Size(480, 47)
        Me.lstExistingBattles.TabIndex = 0
        '
        'btnOK
        '
        Me.btnOK.BackColor = System.Drawing.Color.LightSkyBlue
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.ForeColor = System.Drawing.Color.Black
        Me.btnOK.Location = New System.Drawing.Point(416, 544)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.TabIndex = 18
        Me.btnOK.Text = "Finished"
        '
        'pnlTopBorder
        '
        Me.pnlTopBorder.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.pnlTopBorder.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlTopBorder.Controls.AddRange(New System.Windows.Forms.Control() {Me.picCampaignerIcon, Me.txtTitleBox})
        Me.pnlTopBorder.Name = "pnlTopBorder"
        Me.pnlTopBorder.Size = New System.Drawing.Size(508, 40)
        Me.pnlTopBorder.TabIndex = 17
        '
        'picCampaignerIcon
        '
        Me.picCampaignerIcon.Image = CType(resources.GetObject("picCampaignerIcon.Image"), System.Drawing.Bitmap)
        Me.picCampaignerIcon.Location = New System.Drawing.Point(9, 11)
        Me.picCampaignerIcon.Name = "picCampaignerIcon"
        Me.picCampaignerIcon.Size = New System.Drawing.Size(16, 16)
        Me.picCampaignerIcon.TabIndex = 9
        Me.picCampaignerIcon.TabStop = False
        '
        'txtTitleBox
        '
        Me.txtTitleBox.BackColor = System.Drawing.Color.SteelBlue
        Me.txtTitleBox.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.txtTitleBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.txtTitleBox.ForeColor = System.Drawing.Color.WhiteSmoke
        Me.txtTitleBox.Location = New System.Drawing.Point(32, 8)
        Me.txtTitleBox.Multiline = True
        Me.txtTitleBox.Name = "txtTitleBox"
        Me.txtTitleBox.ReadOnly = True
        Me.txtTitleBox.Size = New System.Drawing.Size(312, 26)
        Me.txtTitleBox.TabIndex = 8
        Me.txtTitleBox.Text = "Campaigner [Assign To Battles]"
        '
        'frmAssignToCombats
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.BackColor = System.Drawing.Color.Black
        Me.ClientSize = New System.Drawing.Size(506, 576)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.pnlMain, Me.btnOK, Me.pnlTopBorder})
        Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.Name = "frmAssignToCombats"
        Me.ShowInTaskbar = False
        Me.pnlMain.ResumeLayout(False)
        Me.pnlTopBorder.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    '#Region "Hidden Members"
    '    Private Shadows Sub ShowDialog()
    '        ' so this isn't visible from outside
    '    End Sub

    '    Private Shadows Sub Show()
    '        ' so this isn't visible from outside
    '    End Sub

    '#End Region

    '#Region "Rules And Reinforcement Assignments"
    '    Private Sub AssignAll()
    '        ' updates a player's choices for rules and reinforcements
    '        ' adds reinforcing units to the selected battle, while removing UNCHECKED units from existing battle (if they were there before)

    '        VerifyAssignedRules()
    '        AssignNewRules()
    '        AssignReinforcements()
    '    End Sub

    '    Private Sub VerifyAssignedRules()
    '        'Dim assignedHT As Hashtable
    '        'Dim assignedRuleKey As String
    '        'Dim asgnRule As AssignedRule

    '        '' check the list for the selected player of previously assigned rules
    '        '' if they are no longer selected, remove them from the list and reassign it to the parent objective/token
    '        '' determine which point of view we are currently using, attacker or defender
    '        'If ActivePlayerPOV() = PlayerPOV_cgd.Attacking Then
    '        '    assignedHT = ptrSelectedBattle.AttackerAssignedRules
    '        'Else
    '        '    assignedHT = ptrSelectedBattle.DefenderAssignedRules
    '        'End If

    '        '' check rules that have previously been assigned to this combat
    '        'For Each assignedRuleKey In assignedHT.Keys
    '        '    ' check to see if this one is still selected in the list
    '        '    asgnRule = assignedHT.Item(assignedRuleKey)
    '        '    Dim lviAsgnRule As New ListViewItem(asgnRule.DisplayName)

    '        '    If lvwSelectedRules.Items.Contains(lviAsgnRule) = True Then
    '        '        ' see if its checked or not

    '        '    End If
    '        'Next
    '    End Sub

    '    Private Sub AssignNewRules()
    '        ' checks each item in the view and assigns the rule if it is checked.
    '        ' all rules in the list have already been checked to make sure they are
    '        ' in range and there is at least one use remaining.

    '    End Sub

    '#End Region

    '#Region "Select and Deselect Rules"
    '    Private Sub SelectRule(ByVal chRule As ChitRuleTreeNode)
    '        ' examines the chitruletreenode and extracts information required
    '        'Dim gc As GroundChitType
    '        Dim asgnRule As AssignedRule

    '        Try
    '            ' find the actual rule in the objective
    '            'gc = ptrGameData.AllChits.Item(chRule.ChitKey)
    '            asgnRule = New AssignedRule()

    '            ' decrement if possible
    '            DecrementIncrementRuleForUse(chRule.Chit, chRule.Ru, True)
    '            ' create an entry in the battle's available rules (we have all the info we need from the chitruletreenode)
    '            asgnRule.BattleAssignedTo = ptrSelectedBattle.BattleID
    '            asgnRule.DisplayName = chRule.Text
    '            asgnRule.OwningChit = chRule.Chit
    '            'asgnRule.OwningChitType = gc.GroundChitClass
    '            asgnRule.Ru = chRule.Ru 'CampaignerGameData.CDGKEY_RULETYPE & chRule.RuleTypeID

    '            ' TODO: determine if a rule has max uses and is being added more than once.
    '            ' determine if a rule of this type already exists (if its a rule with no max uses, we only add it once)

    '            'lstAssignedRules.Items.Add(asgnRule)
    '            ' now, add the rule to the selected battle, for the correct side.
    '            If ActivePlayerPOV() = PlayerPOV_cgd.Attacking Then
    '                ptrSelectedBattle.AttackerAssignedRules.Add(asgnRule)
    '            Else
    '                ptrSelectedBattle.DefenderAssignedRules.Add(asgnRule)
    '            End If
    '        Catch ex As Exception
    '            HandleException(c_Module, ex)
    '        End Try

    '    End Sub

    '    Private Sub DeselectRule()
    '        ' examines the selected assignedRule object in lstAssignedRules and removes the event from
    '        ' the battle, as well as incrementing the rule available uses if required.
    '        Dim assgn As AssignedRule

    '        If Not lstAssignedRules.SelectedItem Is Nothing Then
    '            assgn = lstAssignedRules.SelectedItem
    '            DecrementIncrementRuleForUse(assgn.OwningChit, assgn.Ru, False)
    '            lstAssignedRules.Items.Remove(assgn)
    '        End If

    '    End Sub

    '    Private Sub DecrementIncrementRuleForUse(ByRef gc As ChitMember, ByVal ru As Rule, ByVal decrement As Boolean)
    '        ' examine the objective's rule current and max uses and reduce one if required
    '        'Dim obj As ObjectiveType
    '        'Dim tok As TokenType
    '        Dim chitRule As Rule
    '        'Dim ruleT As RuleType

    '        ' first determine if this is an expendable rule
    '        'ruleT = ptrGameData.RuleTypes_g.Item(CampaignerGameData.CDGKEY_RULETYPE & ruleTypeID)
    '        'If ruleT.MaxUses = 0 Then Exit Sub ' max uses of 0 means it is 'infinite'

    '        If ru.ptrRule.MaxUses = 0 Then Exit Sub

    '        If decrement = True Then
    '            ru.usesRemaining -= 1
    '        Else
    '            ru.usesRemaining += 1
    '        End If

    '        'If gc.UnitType = GroundChitClass_cgd.Objective Then
    '        '    ' find the rule in this guy's list

    '        '    For Each ru In gc.chitRules
    '        '        If objRule.ruleTypeID = ruleT.RuleTypeID Then
    '        '            ' if the rule type id's match, reduce the use count
    '        '            If decrement = True Then
    '        '                objRule.currentUses -= 1
    '        '            Else
    '        '                objRule.currentUses += 1
    '        '            End If
    '        '            Exit For
    '        '        End If
    '        '    Next
    '        'Else
    '        '    tok = gc
    '        '    chitRule = tok.TokenRule
    '        '    If decrement = True Then
    '        '        chitRule.currentUses -= 1
    '        '    Else
    '        '        chitRule.currentUses += 1
    '        '    End If
    '        'End If
    '    End Sub

    '#End Region

    '#Region "Select and Deselect Reinforcements"

    '    Private Sub AssignReinforcements()
    '        Dim nod As TreeNode
    '        Dim unit As ChitMember

    '        Try
    '            For Each nod In tvwReinforcements.Nodes
    '                unit = nod.Tag
    '                If nod.Checked = True Then
    '                    SelectReinforcement(unit)
    '                Else
    '                    DeselectReinforcement(unit)
    '                End If
    '            Next
    '        Catch ex As Exception
    '            HandleException(c_Module, ex)
    '        End Try

    '    End Sub

    '    Private Sub SelectReinforcement(ByRef unit As ChitMember)
    '        ' Ok, if the unit isn't already listed as reinforcing this combat, add it.
    '        If unit.CurrentBattlePosture = BattlePosture_cgd.NoBattle Then
    '            If ActivePlayerPOV() = PlayerPOV_cgd.Attacking Then
    '                unit.CurrentBattlePosture = BattlePosture_cgd.AttackingSupport
    '                unit.battleInvolvedIn = ptrSelectedBattle
    '                unit.Status = GroundUnitStatus_cgd.Reinforcing
    '                ' add to the battle involvedchits
    '                ptrSelectedBattle.InvolvedChits.add(unit)
    '            Else
    '                unit.CurrentBattlePosture = BattlePosture_cgd.Defending
    '                unit.battleInvolvedIn = ptrSelectedBattle
    '                unit.Status = GroundUnitStatus_cgd.Reinforcing
    '                ' add to the battles involvedchits
    '                ptrSelectedBattle.InvolvedChits.add(unit)
    '            End If
    '        End If
    '    End Sub

    '    Private Sub DeselectReinforcement(ByRef unit As ChitMember)
    '        ' if the unit is listed as reinforcing, remove it
    '        If unit.CurrentBattlePosture <> BattlePosture_cgd.NoBattle Then
    '            unit.CurrentBattlePosture = BattlePosture_cgd.NoBattle
    '            unit.battleInvolvedIn = Nothing
    '            unit.Status = GroundUnitStatus_cgd.OK
    '            ptrSelectedBattle.InvolvedChits.remove(unit)
    '        End If
    '    End Sub

    '#End Region

    '    Public Shadows Sub ShowDialog(ByRef gameData As CampaignerGameData)
    '        ptrGameData = gameData

    '        ' load the lists, exit if there are no combats
    '        If ptrGameData.Battles_g.HT.Count = 0 Then
    '            MessageBox.Show("There are no unresolved battles to display.", "No unresolved battles")
    '            Exit Sub
    '        End If

    '        ' load battles list and player list
    '        LoadBattlesList()

    '        MyBase.ShowDialog()
    '    End Sub

    '    Private Sub LoadBattlesList()
    '        Dim key As String
    '        Dim batt As Battle

    '        ClearSubData()

    '        For Each key In ptrGameData.Battles_g.HT.Keys
    '            batt = ptrGameData.Battles_g.Item(key)
    '            lstExistingBattles.Items.Add(batt)
    '        Next

    '    End Sub

    '    Private Sub LoadPlayersList()
    '        ' when a player selects a battle from the battles list, this list is populated with players that have units in the selected combat
    '        If ptrSelectedBattle Is Nothing Then Exit Sub

    '        Dim key As String
    '        'Dim unitKey As String
    '        Dim play As Player
    '        'Dim unit As GroundChitType
    '        Dim fact As Faction

    '        lstPlayer.Items.Clear()
    '        tvwAvailableRules.Nodes.Clear()
    '        lstAssignedRules.Items.Clear()
    '        If tvwReinforcements.Nodes.Count > 0 Then tvwReinforcements.Nodes.Clear()

    '        For Each key In ptrGameData.Players.Keys
    '            play = ptrGameData.Players(key)
    '            For Each mem As ChitMember In ptrSelectedBattle.InvolvedChits

    '                If mem.UnitType = GroundChitClass_cgd.Objective Then
    '                    Dim playtoo As Player
    '                    Dim playKey As String
    '                    fact = mem.OwningFaction
    '                    If playtoo.FactionKey = CampaignerGameData.CDGKEY_FACTION & mem.OwningFaction.FactionID Then
    '                        If lstPlayer.Items.Contains(playtoo) = False Then
    '                            lstPlayer.Items.Add(playtoo)
    '                        End If
    '                    End If
    '                Else
    '                    If mem.OwningPlayer.Equals(play) Then
    '                        lstPlayer.Items.Add(play)
    '                        Exit For
    '                    End If
    '                End If


    '                ' check for same faction I suppose in the case of objectives
    '                'If unit.GroundChitClass = GroundChitClass_cgd.Objective Then
    '                '    ' add all players on this faction
    '                '    Dim playtoo As Player
    '                '    Dim playkey As String
    '                '    fact = ptrGameData.Faction_g.Item(CampaignerGameData.CDGKEY_FACTION & unit.OwningFaction)

    '                '    For Each playkey In ptrGameData.Players.Keys
    '                '        playtoo = ptrGameData.Players.Item(playkey)
    '                '        If playtoo.FactionKey = CampaignerGameData.CDGKEY_FACTION & unit.OwningFaction Then
    '                '            ' add this player if he doesn't already exist
    '                '            If lstPlayer.Items.Contains(playtoo) = False Then
    '                '                lstPlayer.Items.Add(playtoo)
    '                '            End If
    '                '        End If
    '                '    Next

    '                'Else
    '                '    'If unit.OwningPlayer = play.PlayerID Then
    '                '    '    ' add this player to the list and then, jump to next player
    '                '    '    lstPlayer.Items.Add(play)
    '                '    '    Exit For
    '                '    'End If
    '                'End If
    '            Next mem
    '        Next key 'player

    '    End Sub

    '    Private Sub ShowChitsInDirectCombat()
    '        ' shows ALL chits involved in contact combat, not those that are within reinforcement range.
    '        If (ptrSelectedBattle Is Nothing) Or (ptrSelectedPlayer Is Nothing) Then Exit Sub

    '        'Dim key As String
    '        'Dim unit As GroundChitType

    '        tvwInvolvedChits.Nodes.Clear()
    '        tvwObjective.Nodes.Clear()

    '        ' HACK: only considers two opposing factions
    '        For Each mem As ChitMember In ptrSelectedBattle.InvolvedChits
    '            Dim nod As New TreeNode()
    '            ' only list units in direct combat. Those units selected as reinforcing are listed in another box.
    '            nod.Text = mem.Name '& " [" & mem.cu & "]"

    '            If (CampaignerGameData.CDGKEY_FACTION & mem.OwningFaction.FactionID) = ptrSelectedPlayer.FactionKey Then
    '                nod.ForeColor = Color.DarkGreen
    '            Else
    '                nod.ForeColor = Color.DarkRed
    '            End If

    '            tvwInvolvedChits.Nodes.Add(nod)

    '            If mem.UnitType = GroundChitClass_cgd.Objective Then
    '                ' add some info about this objective in the objectivetreeview
    '                ListObjectiveData(DirectCast(mem, InstallationChit))
    '            End If
    '        Next

    '    End Sub

    '    Private Sub ListObjectiveData(ByRef inst As InstallationChit)
    '        'Dim key As String
    '        Dim rule As Rule
    '        'Dim ruleT As RuleType

    '        ' add some info about the objective
    '        Dim topNode As New TreeNode()
    '        topNode.Text = inst.Name

    '        For Each rule In inst.chitRules
    '            Dim ruleNode As New TreeNode()
    '            Dim usesText As String

    '            'ruleT = ptrGameData.RuleTypes_g.Item(CampaignerGameData.CDGKEY_RULETYPE & rule.ruleTypeID)
    '            ruleNode.Text = rule.ptrRule.RuleName & " [" & rule.usesRemaining & "]"
    '            ruleNode.Tag = rule

    '            If rule.ptrRule.MaxUses = 0 Then
    '                usesText = "Unlimited"
    '            Else
    '                usesText = rule.usesRemaining
    '            End If

    '            ruleNode.Nodes.Add("Available Uses: " & usesText)
    '            ruleNode.Nodes.Add("Range: " & rule.ptrRule.BasicRange)
    '            topNode.Nodes.Add(ruleNode)
    '        Next
    '        tvwObjective.Nodes.Add(topNode)

    '    End Sub

    '    Private Sub ShowAvailableReinforcementChits()
    '        ' lists for the player chits that are within reinforcement range of this battle (a property of campaignergamedata)
    '        ' and their current assignment status (reinforceing, ok, etc) Only units with a status of OK can be assigned as reinforcements.
    '        ' units that are already reinforcing this battle are marked with a check. 
    '        ' unchecking a unit marks this unit as no longer supporting the combat, and when the player selects 'Assign', his choices
    '        ' will be set and any UNCHECKED units will be removed from the battle if they exist, while CHECKED units will be added
    '        ' if they don't already exist.
    '        If (ptrSelectedBattle Is Nothing) Or (ptrSelectedPlayer Is Nothing) Then Exit Sub

    '        Dim key As String
    '        Dim unit As UnitType
    '        Dim gc As GroundChitType

    '        If tvwReinforcements.Nodes.Count > 0 Then tvwReinforcements.Nodes.Clear()

    '        ' CLEANUP: break this out from so many if...end if blocks
    '        ' BIGTODO: make a request for an image of the local battlefield, up to the size of reinforcement range so the players know where the battle is occuring.
    '        For Each key In ptrGameData.AllChits.Keys
    '            gc = ptrGameData.AllChits.Item(key)
    '            If gc.GroundChitClass = GroundChitClass_cgd.Unit Then
    '                unit = gc
    '                If unit.OwningPlayer = ptrSelectedPlayer.PlayerID Then
    '                    ' check status. Check distance. If the unit is already engaged in combat, or is supporting another unit, its not listed. 
    '                    If (unit.Status = GroundUnitStatus_cgd.OK) Or (unit.Status = GroundUnitStatus_cgd.Moving) Then
    '                        If WithinReinforcementRange(unit.Location, ptrSelectedBattle.BattleLocation) = True Then
    '                            Dim node As New TreeNode(unit.Name)
    '                            node.Tag = unit.Key
    '                            tvwReinforcements.Nodes.Add(node)
    '                        End If
    '                    ElseIf unit.Status = GroundUnitStatus_cgd.Reinforcing Then
    '                        ' check to see if this unit is reinforcing the SELECTED BATTLE
    '                        If unit.BattleInvolvedInID = ptrSelectedBattle.BattleID Then
    '                            Dim node As New TreeNode(unit.Name)
    '                            node.Tag = unit.Key
    '                            node.Checked = True
    '                            tvwReinforcements.Nodes.Add(node)
    '                        End If
    '                    End If
    '                End If
    '            End If
    '        Next

    '    End Sub

    '    Private Function WithinReinforcementRange(ByRef location As Point, ByRef target As Point) As Boolean
    '        If XYDistance(location.X, target.X, location.Y, target.Y) <= ptrGameData.ReinforcementRange Then
    '            Return True
    '        Else
    '            Return False
    '        End If
    '    End Function

    '    Private Sub ShowAvailableRules()
    '        ' this displays the rules available to the player that are in RANGE of the BATTLE (not specific chits)
    '        Dim gcKey As String
    '        Dim rulKey As String
    '        Dim gc As GroundChitType
    '        Dim obj As ObjectiveType
    '        Dim tok As TokenType
    '        Dim rulet As RuleType
    '        Dim ruleObj As Rule
    '        Dim ruleOK As Boolean

    '        ' show rules already assigned to this battle or not... at least not yet.
    '        ShowAssignedRules()

    '        If tvwAvailableRules.Nodes.Count > 0 Then
    '            tvwAvailableRules.Nodes.Clear()
    '        End If

    '        For Each gcKey In ptrGameData.AllChits.Keys
    '            gc = ptrGameData.AllChits.Item(gcKey)
    '            If gc.GroundChitClass = GroundChitClass_cgd.Objective Then
    '                ' make sure this objective is not under attack

    '                ' check all rules for this objective and see which ones are in range 
    '                obj = gc

    '                If CampaignerGameData.CDGKEY_FACTION & obj.OwningFaction = ptrSelectedPlayer.FactionKey Then
    '                    For Each ruleObj In obj.GCRules
    '                        ' default ruleOK to false
    '                        ruleOK = False
    '                        rulet = ptrGameData.RuleTypes_g.Item(CampaignerGameData.CDGKEY_RULETYPE & ruleObj.ruleTypeID)
    '                        If rulet.AppliesDuring <> RuleApplies_cgd.NonCombat Then
    '                            ' it applies either during combat, or always, so, check other values
    '                            If rulet.MaxUses > 0 Then
    '                                If ruleObj.currentUses > 0 Then
    '                                    ruleOK = True
    '                                Else
    '                                    ruleOK = False
    '                                End If
    '                            Else
    '                                ' there are no maxuses, we're good so far
    '                                ruleOK = True
    '                            End If
    '                            ' if the rule is ok for both applies during and available uses, we'll check range
    '                            If ruleOK Then
    '                                If XYDistance(obj.Location.X, ptrSelectedBattle.BattleLocation.X, obj.Location.Y, ptrSelectedBattle.BattleLocation.Y) <= rulet.BasicRange Then
    '                                    ' add the rule to the list
    '                                    AddAvailableRule(rulet, gcKey, ruleObj.ruleID)
    '                                End If
    '                            End If
    '                        End If
    '                    Next

    '                End If
    '            ElseIf gc.GroundChitClass = GroundChitClass_cgd.Token Then
    '                ' check rule for this token
    '                ' TODO: Tokens

    '            End If
    '        Next

    '    End Sub

    '    Private Sub ShowAssignedRules()
    '        ' find out which player this is, attacker or defender
    '        Dim ptrHT As Hashtable

    '        If ActivePlayerPOV() = PlayerPOV_cgd.Attacking Then
    '            ' player is on the faction attacking side
    '            ptrHT = ptrSelectedBattle.AttackerAssignedRules
    '        Else
    '            ' player is on the faction defending side
    '            ptrHT = ptrSelectedBattle.DefenderAssignedRules
    '        End If

    '        If ptrHT.Count = 0 Then Exit Sub

    '        Dim key As String
    '        Dim asRule As AssignedRule

    '        lstAssignedRules.Items.Clear()

    '        ' display already assigned rules
    '        For Each key In ptrHT.Keys
    '            asRule = ptrHT.Item(key)
    '            lstAssignedRules.Items.Add(asRule)
    '        Next

    '    End Sub

    '    Private Function ActivePlayerPOV() As PlayerPOV_cgd
    '        If ptrSelectedPlayer.FactionKey = CampaignerGameData.CDGKEY_FACTION & ptrSelectedBattle.AttackingFactionID Then
    '            Return PlayerPOV_cgd.Attacking
    '        Else
    '            Return PlayerPOV_cgd.Defending
    '        End If
    '    End Function

    '    Private Sub ClearSubData()
    '        lstPlayer.Items.Clear()
    '        tvwInvolvedChits.Nodes.Clear()
    '        ptrSelectedPlayer = Nothing

    '    End Sub

    '    Private Sub AddAvailableRule(ByRef ruleT As RuleType, ByVal owningChitKey As String, ByVal ruleid As Int32)
    '        Dim gc As GroundChitType = ptrGameData.AllChits(owningChitKey)
    '        Dim ruleTypeNode As New ChitRuleTreeNode("[" & gc.Name & "] " & ruleT.RuleName, owningChitKey, ruleT.RuleTypeID, ruleid)

    '        tvwAvailableRules.Nodes.Add(ruleTypeNode)

    '    End Sub

    '    Private Sub lstAssignedRules_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstAssignedRules.SelectedIndexChanged
    '        If Not lstAssignedRules.SelectedItem Is Nothing Then DeselectRule()
    '        ' repopulate the availablerules
    '        ShowAvailableRules()
    '    End Sub

    '    Private Sub tvwAvailableRules_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvwAvailableRules.MouseDown
    '        Dim chitrule As ChitRuleTreeNode = tvwAvailableRules.GetNodeAt(New Point(e.X, e.Y))
    '        ' when the user clicks a node, it is assigned to the battle (thus reduced from the objective)
    '        SelectRule(chitrule)

    '        ' repopulate the availablerules
    '        ShowAvailableRules()

    '    End Sub

    '    Private Sub lstExistingBattles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstExistingBattles.SelectedIndexChanged
    '        ' populate the players list
    '        ptrSelectedBattle = lstExistingBattles.SelectedItem
    '        If Not ptrSelectedBattle Is Nothing Then
    '            ClearSubData()
    '            LoadPlayersList()
    '        End If

    '    End Sub

    '    Private Sub lstPlayer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPlayer.SelectedIndexChanged
    '        If ptrSelectedBattle Is Nothing Then Exit Sub

    '        ' selecting a player
    '        ptrSelectedPlayer = lstPlayer.SelectedItem

    '        lstAssignedRules.Items.Clear()
    '        tvwAvailableRules.Nodes.Clear()

    '        If ptrSelectedPlayer Is Nothing Then Exit Sub

    '        ' show the chits in direct combat (all, from all sides)
    '        ShowChitsInDirectCombat()
    '        ShowAvailableReinforcementChits()
    '        ShowAvailableRules()

    '    End Sub

    '    Private Sub tvwReinforcements_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwReinforcements.AfterCheck
    '        AssignReinforcements()
    '    End Sub

    '    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    '        Me.Hide()
    '    End Sub
End Class